package de.geckocraft.GeckoEventBroadcaster.Commands;

import de.geckocraft.GeckoEventBroadcaster.Main.EventList;
import de.geckocraft.GeckoEventBroadcaster.Main.Main;
import de.geckocraft.GeckoEventBroadcaster.Main.MainConfig;
import de.geckocraft.GeckoEventBroadcaster.Main.Template;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class Events
  implements CommandExecutor
{
  private Main plugin;

  public Events(Main plugin)
  {
    this.plugin = plugin;
  }

  public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, String[] args)
  {
    if (!(sender instanceof Player))
    {
      sender.sendMessage(ChatColor.RED + "Sie sind kein Spieler!");
      return true;
    }
    Player p = (Player)sender;
    if (cmd.getName().equalsIgnoreCase("event"))
    {
      if (args.length > 0)
      {
        if (args[0].equalsIgnoreCase("list"))
        {
          if ((p.hasPermission("gecko.eventBroadcaster.listadmin")) || (p.hasPermission("gecko.eventBroadcaster.list")))
          {
            eventList(p);
          }
          else
          {
            Template.sendMessage("nopermission", p, new String[0]);
          }
        }
        if (args[0].equalsIgnoreCase("add"))
        {
          if (p.hasPermission("gecko.eventBroadcaster.add"))
          {
            eventAdd(p, args);
          }
          else
          {
            Template.sendMessage("nopermission", p, new String[0]);
          }
        }
        if (args[0].equalsIgnoreCase("remove"))
        {
          if (p.hasPermission("gecko.eventBroadcaster.remove"))
          {
            eventRemove(p, args);
          }
          else
          {
            Template.sendMessage("nopermission", p, new String[0]);
          }
        }
        if (args[0].equalsIgnoreCase("reload"))
        {
          if (p.hasPermission("gecko.eventBroadcaster.reload"))
          {
            Main.loadConfigs();
            Template.sendMessage("reload", p, new String[0]);
          }
          else
          {
            Template.sendMessage("nopermission", p, new String[0]);
          }

        }

      }
      else if ((p.hasPermission("gecko.eventBroadcaster.listadmin")) || (p.hasPermission("gecko.eventBroadcaster.list")))
      {
        showNextEvent(p);
      }
      else
      {
        Template.sendMessage("nopermission", p, new String[0]);
      }

    }

    return false;
  }

  private void eventRemove(Player p, String[] args)
  {
    if (Main.isInteger(args[1]))
    {
      try
      {
        EventList.removeEvent(Integer.parseInt(args[1]));
        Template.sendMessage("removed", p, new String[0]);
      }
      catch (Exception localException)
      {
      }
    }
    else
    {
      Template.sendMessage("nonum", p, new String[] { args[1] });
    }
  }

  private void showNextEvent(Player p)
  {
    if (!EventList.Eventdate.isEmpty())
    {
      Date eventdate = (Date)EventList.Eventdate.get(EventList.Eventnummbers.get(0));
      String eventname = (String)EventList.Eventname.get(EventList.Eventnummbers.get(0));
      Date now = new Date();
      long diferensSek = (eventdate.getTime() - now.getTime()) / 1000L;
      long stundenSek = eventdate.getHours() * 60 * 60;
      long minutenSek = eventdate.getMinutes() * 60;
      long diferensDaySek = diferensSek - stundenSek - minutenSek;

      if (diferensDaySek < 0L)
      {
        if (diferensSek * -1L > MainConfig.getchangetext())
        {
          String time = eventdate.getHours() + ":" + eventdate.getMinutes();
          Template.sendMessage("today", p, new String[] { time, eventname });
        }
        else
        {
          long stundenchach = (diferensSek + 1L) / 60L / 60L;
          long minutenchach = (diferensSek + 1L) / 60L;
          long diferensDaySekchach = diferensSek + 1L;

          while (diferensDaySekchach >= 3600L)
          {
            diferensDaySekchach -= 3600L;
            minutenchach = diferensDaySekchach / 60L;
          }

          String stunden = stundenchach;
          String min = minutenchach;

          if (minutenchach == 0L)
          {
            Template.sendMessage("todayh", p, new String[] { stunden, eventname });
          }
          else if (stundenchach == 0L)
          {
            Template.sendMessage("todaymin", p, new String[] { min, eventname });
          }
          else
          {
            Template.sendMessage("todayhmin", p, new String[] { stunden, min, eventname });
          }
        }
      }
      else if (diferensDaySek < 86400L)
      {
        String time = eventdate.getHours() + ":" + eventdate.getMinutes();
        Template.sendMessage("today", p, new String[] { time, eventname });
      }
      else
      {
        String datum = eventdate.getDay() + "." + eventdate.getMonth() + "." + eventdate.getYear();
        String time = eventdate.getHours() + ":" + eventdate.getMinutes();
        Template.sendMessage("longer", p, new String[] { datum, time, eventname });
      }
    }
    else
    {
      Template.sendMessage("noevent", p, new String[0]);
    }
  }

  private void eventList(Player p)
  {
    HashMap Eventname = EventList.getEventname();
    HashMap Eventdate = EventList.getEventdate();
    ArrayList Eventnummbers = EventList.getEventnummbers();

    if (!Eventname.isEmpty())
    {
      for (int j = 0; Eventnummbers.size() > j; j++)
      {
        Integer eventnummer = (Integer)Eventnummbers.get(j);
        Date datum = (Date)Eventdate.get(eventnummer);
        String eventname = (String)Eventname.get(eventnummer);
        SimpleDateFormat formater = new SimpleDateFormat();
        if (p.hasPermission("gecko.eventBroadcaster.listadmin"))
        {
          Template.sendMessage("atadmin", p, new String[] { eventnummer.toString(), formater.format(datum).toString(), eventname });
        }
        else
        {
          Template.sendMessage("at", p, new String[] { formater.format(datum).toString(), eventname });
        }
      }
    }
    else
    {
      Template.sendMessage("noevent", p, new String[0]);
    }
  }

  private void eventAdd(Player p, String[] args)
  {
    if (p.hasPermission("gecko.EventBroadcaster.add"))
    {
      if (args.length >= 4)
      {
        if (isDate(args[(args.length - 2)]))
        {
          if (isTime(args[(args.length - 1)]))
          {
            String name = "";
            for (int i = 1; i < args.length - 2; i++)
            {
              if (name.equals(""))
              {
                name = args[1];
              }
              else
              {
                name = name + " " + args[i];
              }
            }
            EventList.addEvents(name, args[(args.length - 2)], args[(args.length - 1)]);
            Template.sendMessage("added", p, new String[] { name });
          }
          else
          {
            Template.sendMessage("notime", p, new String[] { args[(args.length - 1)] });
            Template.sendMessage("commandadd", p, new String[0]);
          }
        }
        else
        {
          Template.sendMessage("nodate", p, new String[] { args[(args.length - 2)] });
          Template.sendMessage("commandadd", p, new String[0]);
        }
      }
      else
      {
        Template.sendMessage("commandadd", p, new String[0]);
      }
    }
    else
    {
      Template.sendMessage("nopermission", p, new String[0]);
    }
  }

  private boolean isDate(String s)
  {
    SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");

    Date d = null;
    try
    {
      d = format.parse(s);
    }
    catch (ParseException ex)
    {
      return false;
    }
    return true;
  }

  private boolean isTime(String s)
  {
    SimpleDateFormat format = new SimpleDateFormat("HH:mm");

    Date d = null;
    try
    {
      d = format.parse(s);
    }
    catch (ParseException ex)
    {
      return false;
    }
    return true;
  }
}